import 'dart:convert';
import 'package:dio/dio.dart';

// 根据 userId 获取 Cloth
Future<Response<dynamic>> getClothByUserId({
  required int userId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/cloth/$userId');
  return response;
}

// 根据 userId 和 category 获取 Cloth
Future<Response<dynamic>> getClothByUserIdAndCategory({
  required int userId,
  required String category,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/cloth/$userId/category/$category');
  return response;
}

// 根据 userId 和 subcategory 获取 Cloth
Future<Response<dynamic>> getClothByUserIdAndSubcategory({
  required int userId,
  required String subcategory,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/cloth/$userId/subcategory/$subcategory');
  return response;
}

// 根据 userId 和 userClothId 获取 Cloth
Future<Response<dynamic>> getClothByUserIdAndUserClothId({
  required int userId,
  required int userClothId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/cloth/$userId/userClothId/$userClothId');
  return response;
}

Future<Response<dynamic>> addClothByUserId({
  required int userId,
  required Map<String, dynamic> clothData,
  required Dio dio,
  String? imagePath, // 允许传递空值
}) async {
  // 创建一个包含 clothDTO 的 FormData 对象
  final Map<String, dynamic> formDataMap = {
    'clothDTO': jsonEncode(clothData), // 将 clothData 转为 JSON 字符串
  };

  // 如果 imagePath 非空，则添加 imageFile
  if (imagePath != null && imagePath.isNotEmpty) {
    formDataMap['imageFile'] = await MultipartFile.fromFile(imagePath, filename: imagePath.split('/').last);
  }

  // 创建 FormData 对象
  FormData formData = FormData.fromMap(formDataMap);

  // 设置请求选项
  Options options = Options(
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  );

  try {
    // 发送 POST 请求
    Response<dynamic> response = await dio.post(
      '/cloth/$userId',
      data: formData,
      options: options,
    );
    return response;
  } catch (e) {
    print('Error: $e');
    throw e;
  }
}

// 根据 userId 和 userClothId 删除 Cloth
Future<Response<dynamic>> deleteClothByUserIdAndUserClothId({
  required int userId,
  required int userClothId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.delete('/cloth/$userId/$userClothId');
  return response;
}

// 根据 userId 和 userClothId 更新 Cloth
Future<Response<dynamic>> updateClothByUserIdAndUserClothId({
  required int userId,
  required int userClothId,
  String? clothName,
  String? imageUrl,
  String? category,
  String? subcategory,
  required Dio dio,
}) async {
  Map<String, dynamic> queryParams = {};
  if (clothName != null) queryParams['clothName'] = clothName;
  if (imageUrl != null) queryParams['imageUrl'] = imageUrl;
  if (category != null) queryParams['category'] = category;
  if (subcategory != null) queryParams['subcategory'] = subcategory;

  Response<dynamic> response = await dio.put(
    '/cloth/$userId/$userClothId',
    queryParameters: queryParams,
  );
  return response;
}
